<?php

namespace app\common\model;

use think\Db;
use think\Config;
use think\Model;
use com\gdl\agk\interfaces\UserServiceClient;

class TrainingRoom extends Model
{

    // 指定表名,不含前缀
    protected $name = 'training_room';

    /* 获取USER表中人所有用户到培训室 */

    public function getAllUserToBasic($trainingid, $userid = 0)
    {
        if (empty($trainingid))
            return false;
        try {
            $sql = "replace into ex_training_user(identity_id,username,trainingid,usertruename,company,idcard,departmentid) ";
            $sql .= "select uc.id,u.username,{$trainingid},u.realname,uc.company_name,u.idcard,u.departmentid ";
            $sql .= "from ex_user_company as uc left join ex_user as u on uc.userid=u.userid "; //排除超级管理员
            $sql .= "where uc.admin_reg='{$_SESSION['think']['reg']}' and uc.userid>0 and uc.status=0 ";
            if (!empty($userid)) {
                if (is_array($userid))
                    $userid = implode(',', $userid);
                $sql .= " and uc.userid in ('{$userid}') ";
            }
            $sql .= "and uc.id not in (select identity_id from ex_training_user where trainingid=" . $trainingid;

            if (!empty($userid))
                $sql .= " and userid in('{$userid}')";
            $sql .= ")";
            $this->execute($sql);
        } catch (Exception $ex) {
            \SLogger::getLogger();
            return FALSE;
        }
        return TRUE;
    }

    public function importUserById($trainingid, $userid)
    {
        if (empty($trainingid))
            return false;
        if (empty($userid))
            return false;
        if (is_array($userid))
            $userid = implode(',', $userid);

        $sql = "insert into ex_training_user(identity_id,username,trainingid,usertruename,company,idcard,departmentid) ";
        $sql .= "select uc.id,u.username,{$trainingid},u.realname,uc.company_name,u.idcard,u.departmentid ";
        $sql .= "from ex_user_company as uc left join ex_user as u on uc.userid=u.userid ";
        $sql .= "where uc.admin_reg='{$_SESSION['think']['reg']}' and uc.userid>0 and uc.id in({$userid }) and uc.status=0 ";
        $sql .= "and uc.id not in (select ex_training_user.identity_id from ex_training_user where trainingid={$trainingid} and identity_id in('{$userid}'))";
        $this->execute($sql);
        return true;
    }

    /* 根据项目组ID获取USER表中人所有用户到培训室 */

    public function getAllUserToBasicByProjecttid($trainingid, $projecttid)
    {
        if (empty($trainingid))
            return false;
        if (empty($projecttid))
            return false;
        /* 获取项目组下的用户ID */

        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk'));
            $userService = new UserServiceClient(NULL);
            \ApiClient::build($userService);
            $r = $userService->getAllUserByProjecttid($projecttid, $_SESSION['think']['reg']);

        } catch (Exception $ex) {
            \SLogger::getLogger('job_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }

        if (empty($r->data))
            return 0;
        foreach ($r->data as $key => $val) {
            $result[] = $val['id'];
        }
        return $this->importUserById($trainingid, $result);
    }

    /* 根据专业ID获取USER表中人所有用户到培训室 */

    public function getAllUserToBasicBySpecialtyid($trainingid, $specialtyid) 
    {
        if (empty($trainingid))
            return false;
        if (empty($specialtyid))
            return false;
        /* 获取项目组下的用户ID */
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk'));
            $userService = new UserServiceClient(NULL);
            \ApiClient::build($userService);
            $r = $userService->getAllUserBySpecialtyid($specialtyid, $_SESSION['think']['reg']);
        } catch (Exception $ex) {
            \SLogger::getLogger('job_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        if (empty($r->data))
            return 0;
        foreach ($r->data as $key => $val) {
            $result[] = $val['id'];
        }
        return $this->importUserById($trainingid, $result);
    }

    /**
     * @param $id
     */
    public function getUserByTrainingid($id, $name = array())
    {
        $conent = ['trainingid' => $id];
        if (!empty($name)) {
            $conent = $name;
            $conent['trainingid'] = $id;
        }
        return Db::name('training_user')->where($conent)->select();
    }

    /**
     * @param $id
     */
    public function getTrainingInfoByTrainingid($id)
    {
        return $this->where(['id' => $id, 'isdelete' => 0, 'status' => 1])->find();
    }
    public function getTrainingIdBybasicid($id)
    {
        return $this->where(['examid' => $id, 'isdelete' => 0, 'status' => 1])->field('id')->find();
    }

}
